package com.hunttown.mes.common.utils;

import java.math.BigDecimal;

/**
 * created by wangjunfu on 2018/11/1
 */
public class ConvertUtils {

    /**
     * 将字符串转化成整数
     *
     * @param s 字符串
     * @return 整数
     */
    public static int stringToInt(String s) {
        if (s == null || s.equals("")) {
            return 0;
        }

        if (s.length() > 10) {  // 简单判断一下
            return 0;
        }

        double tmp = Double.valueOf(s);

        return (int) tmp;
    }

    /**
     * 将字符串转化成Double
     *
     * @param s 字符串
     * @return double
     */
    public static double stringToDouble(String s) {
        if (s == null || s.equals("")) {
            return 0;
        }

        if (s.length() > 38) {  // 简单判断一下
            return 0;
        }

        return Double.valueOf(s);
    }

    /**
     * 将分钟转换成 “x天x时x分” 格式
     *
     * @param minutes 分
     * @return
     */
    public static String minutesToFormat(Integer minutes) {
        if (minutes == null || minutes <= 0) {
            return "0分";
        }

        String result = "";

        Integer day = minutes / (24 * 60);
        if (day > 0) {
            result = day + "天";
        }

        Integer surplus = minutes % (24 * 60);
        Integer hour = surplus / 60;
        if (hour > 0) {
            result += hour + "时";
        }

        Integer min = surplus % 60;
        if (min > 0) {
            result += min + "分";
        }

        return result;
    }

    //将字符串转化为Decimal
    public static BigDecimal strToDecimal(String str) {
        if (StringUtils.isBlank(str)) {
            return BigDecimal.ZERO;
        }

        return new BigDecimal(str);
    }

    /*
     * 四舍五入用 Math.round(double a)；
     * 向上取整用 Math.ceil(double a)；
     * 向下取整用 Math.floor(double a)；
     * */
    //将秒转化为分（向上取整用）
    public static Integer secondsToMinute(Integer seconds) {
        if (seconds == null) {
            return 0;
        }

        float m = (float) seconds / 60;
        return (int) Math.ceil(m);
    }

    /**
     * 字符串转换成为16进制(无需Unicode编码)
     *
     * @param str 字符串
     * @return 十六进制
     */
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (byte b : bs) {
            bit = (b & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = b & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString().trim();
    }

    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     *
     * @param hexStr 十六进制
     * @return 字符串
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    public static void main(String[] args) throws Exception {
        System.out.println("hex：" + str2HexStr("hex"));
        System.out.println("str：" + hexStr2Str("303131333133303331"));
    }
}
